package org.pictual.service;

import java.util.List;

import org.apache.log4j.Logger;
import org.pictual.business.dao.DAOFactory;
import org.pictual.business.dao.MySQLDAOFactory;
import org.pictual.business.dao.SequenceGeneratorDAO;
import org.pictual.business.dao.exceptions.DAOException;
import org.pictual.business.vo.CompletoVO;
import org.pictual.business.vo.IdiomaVO;
import org.pictual.util.PalabraEnTabla;


public class IdiomaDelegateImpl implements IdiomaDelegate{

	private static IdiomaDelegateImpl _instance = null;
	protected static Logger log= Logger.getLogger(IdiomaDelegateImpl.class);
	protected SequenceGeneratorDAO sequenceGeneratorDAO;
	
	private IdiomaDelegateImpl(){}

	public static IdiomaDelegateImpl getInstance(){
		if(_instance==null){
    		_instance = new IdiomaDelegateImpl();
    	}
        return _instance;
	}

	public IdiomaVO readIdioma(String nombre, int idioma) {
		try {
			IdiomaVO idiomaVO = DAOFactory.getDAOFactory(DAOFactory.MYSQL).getIdiomaDAO().readIdioma(nombre, idioma);
//			MySQLDAOFactory.closeConnection();
			return idiomaVO;
		} catch (DAOException e) {
			log.error("readIdioma: DAOException: ",e);
		}
		return null;
	}

	public void insertIdioma(IdiomaVO idioma) {
		try {
			DAOFactory.getDAOFactory(DAOFactory.MYSQL).getIdiomaDAO().insertIdioma(idioma);
//			MySQLDAOFactory.closeConnection();
		} catch (DAOException e) {
			log.error("insertIdioma: DAOException: ",e);
		}
	}
	
	public void setSequenceGeneratorDAO(SequenceGeneratorDAO sequenceGeneratorDAO) {
		this.sequenceGeneratorDAO = sequenceGeneratorDAO;
	}

	public List<CompletoVO> selectListado(String wiki, String order, String images) throws DAOException {
		return DAOFactory.getDAOFactory(DAOFactory.MYSQL).getIdiomaDAO().selectListado(wiki, order, images);
	}

	public CompletoVO selectPalabraCompleta(String palabra, int idioma, int wiki)
			throws DAOException {
		try {
			CompletoVO completoVO = DAOFactory.getDAOFactory(DAOFactory.MYSQL).getIdiomaDAO().selectPalabraCompleta(palabra, idioma, wiki);
//			MySQLDAOFactory.closeConnection();
			return completoVO;
		} catch (DAOException e) {
			log.error("selectPalabraCompleta: DAOException: ",e);
		}
		return null;
	}

	@Override
	public int selectIdPalabra(String palabra) throws DAOException {
		try {
			int idPalabra = DAOFactory.getDAOFactory(DAOFactory.MYSQL).getIdiomaDAO().selectIdPalabra(palabra);
//			MySQLDAOFactory.closeConnection();
			return idPalabra;
		} catch (DAOException e) {
			log.error("selectIdPalabra: DAOException: ",e);
		}
		return 0;
	}

	@Override
	public int aumentarVisitas(IdiomaVO idiomaVO) {
		try {
			return DAOFactory.getDAOFactory(DAOFactory.MYSQL).getIdiomaDAO().aumentarVisitas(idiomaVO);
		} catch (DAOException e) {
			log.error("aumentarVisitas: DAOException: ",e);
		}
		return 0;
	}

	@Override
	public List<CompletoVO> selectlistadoMasVisitadas(int visitas)
			throws DAOException {
		return DAOFactory.getDAOFactory(DAOFactory.MYSQL).getIdiomaDAO().selectlistadoMasVisitadas(visitas);

	}
}
